
/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************/


#include "bsp.h"
#include "LL_Gui.h"
#include "Game.h"
#include "Timer.h"
#include "Menu.h"
#include "bsp_timer.h"
#include "bsp_usart.h"
#include "bsp_JQ8900.h"

//ADC
uint16_t temp_adc_value;
float AD_Value;

uint8_t  CurrMode;         //当前模式
uint8_t  NextMode;         //下个模式
uint8_t  ReturnFlag;       //返回主菜单标志
uint8_t  OverFlag;         //游戏结束标志
uint8_t  OverflowFlag;     //溢出标志
uint16_t BestScore;        //历史最高分数
uint16_t CurrScore;        //当前分数

//--locale=english
uint8_t buf[EE_SIZE];	/* 数据测试用缓冲区 16KB */
uint8_t music_choice = 0;










void test2(void)
{

    Timer_Init();
    
    Game_Init(); // 直接初始化Game
    NextMode = 0x02; // 设置当前模式为Game

    while (1)
    {
        if (NextMode != 0x02) // 只处理Game
        {
            Key_Clear();
            NextMode = 0x02; // 确保一直保持在Game
        }
        else
        {
            NextMode = Game_Loop(); // 执行Game主循环
        }
    }
}


void test3(void){
  NextMode = 0x02;
  CurrMode = NextMode ;
  
	while(1)
	{
		if(NextMode != CurrMode)
		{
			Key_Clear();
			CurrMode = NextMode;
			switch(NextMode)        //初始化部分，只在界面切换时执行一次，各模块初始化函数
			{


				case 0x02: {Game_Init(); break;}

				default:   
				{
					OLED_DrawRectangle(0, 0, 128, 64, OLED_UNFILLED);

					OLED_Update();
					break;
				}
			}
		}
		else
		{
			switch(CurrMode)    //循环执行部分，各模块主函数
			{

				case 0x02: {NextMode = Game_Loop(); break;}

			}
		}
	}
}




void test(void) 
{
    // 初始化系统和其他必要的设置

    while(1)
    {
        // 只保留 Game 的初始化和循环
        Game_Init();  // 初始化 Game

        while(1)  // Game 主循环
        {
            NextMode = Game_Loop();  // 运行 Game 的主循环
            if (NextMode != 0x02)  // 如果退出 Game
            {
                break; // 退出内部循环，返回主循环
            }
        }
    }
}




int main(void)
{
  
  uint8_t writeBuffer[2]; // 因为 uint16_t 占 2 字节
   uint8_t readBuffer[2];

  
	uint8_t jq8900cmd[] = {0xAA,0x02,0x00,0xAC};
	uint8_t ucKeyCode;	
    uint8_t temp = 0;
	uint32_t freq = 1500;
	uint16_t adc;
	u16 colorTemp=0;
	u16 ColorTab[5]={RED,GREEN,BLUE,YELLOW,BRED};//定义颜色数组
	u8 key;
	u16 i=0;
	u16 j=0;
  
  
  CurrMode = 0;
  music_choice = 1;
	bsp_Init();
  USART_Config();
  JQ8900setup();
  Timer_Init();
  oled_Init(); 
  BEEP_InitHard();
  
  
 
  while(1)
  {
      
    switch(CurrMode)
    {
      
      
      
      
      case 0:      //开始界面
        //menu                         //return 0
            Start_Menu();
            
            CurrMode = Start_Control();     //  模式选择判断 return 1
              if(music_choice == 1) //音乐选择
              {
                JQ8x00_Command_Data(AppointTrack,music_choice);
                music_choice = 0;
              }
            
      
            if(CurrMode == 1)    //模式切换逻辑处理
            {
              Game_Init();
              music_choice = 2;
            }
            
           break;
      case 1:             //游戏界面
              bsp_ClearKey();
              Game_Loop();          //return 2  模式选择判断
      
      
      
              if(music_choice == 2)    //音乐选择
              {
                JQ8x00_Command_Data(AppointTrack,music_choice);
                music_choice = 0;
              }
              
              if(ReturnFlag == 1)           //模式切换逻辑处理
              {
                  ReturnFlag = 0;
                  CurrMode = 2;
                  oled_Clear();
                  //JQ8x00_AppointPlay(JQ8X00_Symbol MODE, char *DATA);
                music_choice = 3;
                
                  
              }
              break;
              
              
      case 2:               //退出界面
        
      
               writeBuffer[0] = (BestScore >> 8) & 0xFF; // 高字节
              writeBuffer[1] = BestScore & 0xFF;        // 低字节
              ee_WriteBytes(writeBuffer, 0x00, sizeof(writeBuffer));
              ee_ReadBytes(readBuffer, 0x00, sizeof(readBuffer)) ;
              BestScore = (readBuffer[0] << 8) | readBuffer[1]; 
      
              JQ8x00_Command_Data(AppointTrack,music_choice);
              oled_ShowString(1,1,"Max Score:");
              oled_ShowString(1,3,"Last Score:");
              oled_ShowNum(100,1,BestScore,4,2);
              oled_ShowNum(100,3,CurrScore,4,2);
              bsp_DelayMS(4000);

              Clear_OLED_GRAM();
              oled_Clear();  
              music_choice = 1;     //音乐选择
              CurrMode = 0;
              
    
    }

  }
  
  
}





#if 0
GPIO input test
		if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_8) == Bit_RESET)
		{
			bsp_LedOn(2);
		}else{
			bsp_LedOff(2);
		}






/* 按键口对应的RCC时钟 */
#define RCC_KEY 	(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD)
#endif




void TIM2_IRQHandler(void)
{
    AdcPro();                //电压采集
    Key_Tick();
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET && CurrMode == 1)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // 清除中断标志
        //Key_Tick(); // 处理按键输入
        Game_Tick(); // 更新Game的逻辑
    }
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update); 
}



